// pages/mine/mine.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    myName:"",
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    selfmessage:"",
    hiddenmodalput:true,
    imgHead:"https://www.304cloud.top:4321"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.setNavigationBarTitle({
      title: "我的主页",
    })
    let userId = wx.getStorageSync('openid')
    this.setData({
      userId:userId
    })
    /**
     * 获取当前设备的宽高
     */
    wx.getSystemInfo({ 
      success: function (res) { 
          that.setData({ 
              clientHeight: res.windowHeight 
          }); 
      } 
  })
    this.getZanguo();
    this.getGeren();
    this.getJianjie();
    this.getNum();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.getUserInfo({
      lang: 'zh_CN',
      success:res => {
        console.log(res);
        let uinfo = res.userInfo;
        this.setData({
          myHeadImage:uinfo.avatarUrl,
          myName:uinfo.nickName
        })
      }
    })
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 4
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  backTo:function(){
    wx.navigateTo({
      url: '../../pages/setting/setting',
    })
  },
  getUserInfo: function(e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
   //  tab切换逻辑
  swichNav: function( e ) {
    var that = this;
    if( this.data.currentTab === e.target.dataset.current ) {
        return false;
    } else {
        that.setData( {
          currentTab: e.target.dataset.current
        })
    }
  },
  bindChange: function( e ) {
    var that = this;
    that.setData( { 
      currentTab: e.detail.current 
    });
  },
  getZanguo:function(){
    let zanPoemList=[];
    let zanguoList = [];
    wx.request({
      url : "https://www.304cloud.top:4321/zanguo",
      method: "GET",
      data: {
        user_id:this.data.userId
      },
      header: {
        "Content-Type": "application/json"
      },
      success: (res)=> {
        console.log(res.data.data);
        this.setData({
          zanguoList:res.data.data
        })
        zanguoList = res.data.data;
        for(var i=0;i<zanguoList.length;i++){
          zanPoemList.push(zanguoList[i].article_txt.replace(/。/g,'\n').replace(/，/g,'\n'))
        }
        this.setData({
          zanPoemList:zanPoemList,
          imgHead:"https://www.304cloud.top:4321"
        })
      }
    })
  },
  getGeren:function(){
    let grPoemList=[];
    let gerenList = [];
    wx.request({
      url : "https://www.304cloud.top:4321/self",
      method: "GET",
      data: {
        user_id:this.data.userId
      },
      header: {
        "Content-Type": "application/json"
      },
      success: (res)=> {
        console.log(res.data.data);
        this.setData({
          gerenList:res.data.data
        })
        gerenList = res.data.data;
        for(var i=0;i<gerenList.length;i++){
          grPoemList.push(gerenList[i].article_txt.replace(/。/g,'\n').replace(/，/g,'\n'))
        }
        this.setData({
          grPoemList:grPoemList,
          imgHead:"https://www.304cloud.top:4321"
        })
      }
    })
  },
  getJianjie:function(){
    let jianjie='';
    wx.request({
      url : "https://www.304cloud.top:4321/selfmessage",
      method: "GET",
      data: {
        user_id:this.data.userId
      },
      header: {
        "Content-Type": "application/json"
      },
      success: (res)=> {
        console.log(res.data.data);
        this.setData({
          selfmessage:res.data.data
        })
      }
    })
  },
  ggJianjie:function(){
    this.setData({
      hiddenmodalput:!this.data.hiddenmodalput
    })
  },
  cancel:function(){
    this.setData({
      hiddenmodalput:true
    })
  },
  inputvalue:function(e){
    this.setData({
      newJianjie:e.detail.value
    })
  },
  confirm:function(){
    this.setData({
      hiddenmodalput:true,
    })
    wx.request({
      url : "https://www.304cloud.top:4321/changeselfmessage",
      method: "POST",
      data: {
        user_id:this.data.userId,
        user_self:this.data.newJianjie
      },
      header: {
        "Content-Type": "application/json"
      },
      success: (res)=> {
        console.log(res);
        this.onLoad();
      }
    })
    if(this.data.newJianjie.length>20){
      wx.showToast({
        title: '诶哟超出字数了,请重新更改',
        duration: 2000,
        icon:'none'
      })
    }else{
      this.setData({
        selfmessage:this.data.newJianjie
      })
    }
  },
  goToMyDetail:function(e){
    let id=e.currentTarget.dataset.id;
    console.log(id)
    wx.navigateTo({
        url: '/pages/mydetail/mydetail?id='+id
    })
  },
    getNum:function(){
      wx.request({
        url: 'https://www.304cloud.top:4321/huozan',
        method:"GET",
        data:{
            user_id:this.data.userId
        },
        header:{
          'content-type':'application/json',
        },
        success:(res)=>
        {
          console.log(res.data.data)
          this.setData({
            fanNum:res.data.data
          })
        }
    })
      wx.request({
          url: 'https://www.304cloud.top:4321/getguanzhu',
          method:"GET",
          data:{
              user_id:this.data.userId
          },
          header:{
            'content-type':'application/json',
          },
          success:(res)=>
          {
            console.log(res)
            this.setData({
              followNum:res.data.data.length
            })
          }
      })
      wx.request({
        url: 'https://www.304cloud.top:4321/fans',
        method:"GET",
        data:{
            user_id:this.data.userId
        },
        header:{
          'content-type':'application/json',
        },
        success:(res)=>
        {
          this.setData({
            fansNum:res.data.data.length
          })
        }
    })
  },
  goToTieziDetail:function(e){
    let tieziId=e.currentTarget.dataset.id;
    console.log(tieziId)
    wx.navigateTo({
        url: '/pages/tiezi/tiezi?id=' +tieziId
    })
  }
})